package com.example.springbooteasycode.utils.tablepdf.bad;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 生成Word文档
 * @author: boykchen
 */
public class SurveyResultWordStyleGenerator {

    public static void main(String[] args) throws IOException {
        XWPFDocument document = new XWPFDocument();

        // ===============================设置最上方的 基础信息===============================
        // 添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.createRun().setText("物业服务质量满意度年度调查");
        titleParagraph.setSpacingBefore(720); // 增加上间距
        titleParagraph.setSpacingAfter(360); // 减少下间距
        titleParagraph.setBorderBottom(Borders.DOUBLE); // 设置底部边框
        titleParagraph.setBorderTop(Borders.NONE); // 取消顶部边框
        titleParagraph.setBorderLeft(Borders.NONE); // 取消左侧边框
        titleParagraph.setBorderRight(Borders.NONE); // 取消右侧边框
        titleParagraph.setAlignment(ParagraphAlignment.CENTER); // 设置居中对齐
        titleParagraph.getRuns().get(0).setFontSize(16); // 设置字体大小为24号

        // 添加时间行
        XWPFParagraph timeParagraph = document.createParagraph();
        timeParagraph.createRun().setText("收集时间：2024-10-12 16:41:06 至 2024-10-14 09:12:14");
        timeParagraph.setAlignment(ParagraphAlignment.LEFT);

        // 添加发布者
        XWPFParagraph publisherParagraph = document.createParagraph();
        publisherParagraph.createRun().setText("发布者：新物业");
        publisherParagraph.setAlignment(ParagraphAlignment.LEFT);

        // 添加收集份数
        XWPFParagraph collectionCountParagraph = document.createParagraph();
        collectionCountParagraph.createRun().setText("收集份数：4 份");
        collectionCountParagraph.setAlignment(ParagraphAlignment.LEFT);

        // 添加时间行
        XWPFParagraph fenshu = document.createParagraph();
        XWPFRun timeRun = fenshu.createRun();
        timeRun.setText("分数：100");
        timeRun.setColor("0000FF"); // 设置字体颜色为蓝色（十六进制颜色代码）
        timeParagraph.setAlignment(ParagraphAlignment.LEFT);


        // ===============================添加统计结果段落===============================
        XWPFParagraph statisticsParagraph = document.createParagraph();
        statisticsParagraph.createRun().setText("统计结果\n"); // 使用换行符分隔文字
        statisticsParagraph.createRun().addBreak(); // 插入空白行
        statisticsParagraph.createRun().setText("物业服务满意度2024年度调查收集已完成，共4题，有效收集份数：4份，收集时间：\n2024-10-12 16:41:06至2024-10-14 09:12:14");
        statisticsParagraph.setSpacingBefore(360); // 增加上间距
        statisticsParagraph.setSpacingAfter(360); // 增加下间距
        statisticsParagraph.setBorderBottom(Borders.SINGLE); // 设置底部边框
        statisticsParagraph.setBorderTop(Borders.SINGLE); // 设置顶部边框
        statisticsParagraph.setBorderLeft(Borders.SINGLE); // 设置左侧边框
        statisticsParagraph.setBorderRight(Borders.SINGLE); // 设置右侧边框
        statisticsParagraph.setAlignment(ParagraphAlignment.LEFT); // 设置左对齐

        // 添加单选题表格
        addSingleChoiceTableToDocument(document);

        // 添加多选题表格
        addMultiChoiceTableToDocument(document);

        // 添加填空题表格
        addFillInTheBlankTableToDocument(document);

        // 输出到文件
        try (FileOutputStream out = new FileOutputStream("/Users/boykchen/Desktop/survey_results-new.docx")) {
            document.write(out);
        }

    }

    private static void addSingleChoiceTableToDocument(XWPFDocument document) {
        XWPFParagraph singleChoiceTitleParagraph = document.createParagraph();
        singleChoiceTitleParagraph.createRun().setText("1. 卫生满意度【单选题】");
        singleChoiceTitleParagraph.setAlignment(ParagraphAlignment.LEFT); // 设置左对齐

        // 实现单选题表格的创建
        XWPFTable table = document.createTable(4, 3);

        // 设置表头
        table.getRow(0).getCell(0).setText("选项");
        table.getRow(0).getCell(1).setText("小计");
        table.getRow(0).getCell(2).setText("比例");

        // 设置非常满意行
        table.getRow(1).getCell(0).setText("非常满意");
        table.getRow(1).getCell(1).setText("0");
        table.getRow(1).getCell(2).setText("0%");

        // 设置满意行
        table.getRow(2).getCell(0).setText("满意");
        table.getRow(2).getCell(1).setText("2");
        table.getRow(2).getCell(2).setText("50%");

        // 设置不满意行
        table.getRow(3).getCell(0).setText("不满意");
        table.getRow(3).getCell(1).setText("2");
        table.getRow(3).getCell(2).setText("50%");
    }

    private static void addMultiChoiceTableToDocument(XWPFDocument document) {
        XWPFParagraph multiChoiceTitleParagraph = document.createParagraph();
        multiChoiceTitleParagraph.createRun().setText("2. 优秀员工投票(可多选)【多选题】");
        multiChoiceTitleParagraph.setAlignment(ParagraphAlignment.LEFT); // 设置左对齐

        // 实现多选题表格的创建(rows = 几行 ， cols = 几列)
        XWPFTable table = document.createTable(4, 3);

        // 设置表头
        table.getRow(0).getCell(0).setText("选项");
        table.getRow(0).getCell(1).setText("小计");
        table.getRow(0).getCell(2).setText("比例");

        // 设置非常满意行
        table.getRow(1).getCell(0).setText("张三");
        table.getRow(1).getCell(1).setText("2");
        table.getRow(1).getCell(2).setText("25%");

        // 设置满意行
        table.getRow(2).getCell(0).setText("王五");
        table.getRow(2).getCell(1).setText("2");
        table.getRow(2).getCell(2).setText("25%");

        // 设置不满意行
        table.getRow(3).getCell(0).setText("小q");
        table.getRow(3).getCell(1).setText("4");
        table.getRow(3).getCell(2).setText("50%");
    }

    private static void addFillInTheBlankTableToDocument(XWPFDocument document) {
        // 实现填空题创建
        XWPFParagraph fillInTheBlankTitleParagraph = document.createParagraph();
        fillInTheBlankTitleParagraph.createRun().setText("3. 你对物业对意见建议" + "\n");
        fillInTheBlankTitleParagraph.setAlignment(ParagraphAlignment.LEFT); // 设置左对齐
        fillInTheBlankTitleParagraph.createRun().addBreak(); // 插入空白行
        fillInTheBlankTitleParagraph.createRun().setText("共4份数据");

    }
}
